﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.IO.IsolatedStorage;
using System.Diagnostics;
using System.Collections.Generic;
using System.Runtime.Serialization;

namespace Config
{
    [DataContract]
    public class Topic
    {
        [DataMember]
        public float Value { get; set; }

        [DataMember]
        public string Name { get; set; }
    }

    public class AppSettings
    {
        private IsolatedStorageSettings isolatedStore;

        public AppSettings()
        {
            try
            {
                isolatedStore = IsolatedStorageSettings.ApplicationSettings;
            }
            catch (Exception e)
            {
                Debug.WriteLine("Exception while using IsolatedStorageSettings: " + e.ToString());
            }
        }

        public valueType GetValueOrDefault<valueType>(string Key, valueType defaultValue)
        {
            valueType value;
            try
            {
                value = (valueType)isolatedStore[Key];
            }
            catch (KeyNotFoundException)
            {
                value = defaultValue;
            }
            catch (ArgumentException)
            {
                value = defaultValue;
            }
            return value;
        }


        public bool AddOrUpdateValue(string Key, Object value)
        {
            bool valueChanged = false;
            try
            {
                // if new value is different, set the new value.
                if (isolatedStore[Key] != value)
                {
                    isolatedStore[Key] = value;
                    valueChanged = true;
                }
            }
            catch (KeyNotFoundException)
            {
                isolatedStore.Add(Key, value);
                valueChanged = true;
            }
            catch (ArgumentException)
            {
                isolatedStore.Add(Key, value);
                valueChanged = true;
            }
            catch (Exception e)
            {
                Debug.WriteLine("Exception while using IsolatedStorageSettings: " + e.ToString());
            }
            return valueChanged;
        }

        public void Save()
        {
            isolatedStore.Save();
        }

        public bool EnableGinga
        {
            get
            {
                return GetValueOrDefault<bool>("EnableGinga", false);
            }
            set
            {
                 AddOrUpdateValue("EnableGinga", value);
            }
        }



        public List<Topic> Topics
        {
            get
            {
                return GetValueOrDefault<List<Topic>>("Topics", new List<Topic>());
            }
            set
            {
                AddOrUpdateValue("Topics", value);
            }
            
        }

        public int? LastChannel
        {
            get
            {
                return GetValueOrDefault<int?>("LastChannel", null);
            }
            set
            {
                AddOrUpdateValue("LastChannel", value);
            }
        }

        public int? AutoPlay
        {
            get
            {
                return GetValueOrDefault<int?>("AutoPlay", null);
            }
            set
            {
                AddOrUpdateValue("AutoPlay", value);
            }
        }


    }
}
